package services

import (
	"github.com/skip2/go-qrcode"
	"gorm.io/gorm"
	"log"
	"path/filepath"
	"server/global"
	"server/internal/model"
	"server/internal/pkg/qrcodes"
	"server/utils/tools"
	"strconv"
	"time"
)

var Tissue = new(tissue)

type tissue struct {
	model.Tissue
	Children []tissue `json:"children,omitempty" gorm:"-"`
}

// GetList 树状图
func (a tissue) GetList(pid int) []tissue {
	Db := global.GL_DB
	var menus []tissue

	Db.Model(&menus).Where("pid=?", pid).Find(&menus)
	if len(menus) == 0 {
		return nil
	}

	var list []tissue
	for _, v := range menus {

		v.Children = a.GetList(v.ID)
		list = append(list, v)
	}

	return list
}

// GetById 单条
func (a tissue) GetById(id any) tissue {
	Db := global.GL_DB
	detail := tissue{}
	Db.Model(&a).Where("id =?", id).First(&detail)

	return detail

}

// Create 新增
func (a tissue) Create(data model.Tissue) (model.Tissue, error) {
	Db := global.GL_DB

	t := time.Now()
	data.CreateTime = int(t.Unix())
	data.UpdateTime = int(t.Unix())

	Db = Db.Model(&data).Create(&data)

	//a.GenerateQrcode(a.ID)

	return data, Db.Error
}

// Update 编辑
func (a tissue) Update(data model.Tissue) (model.Tissue, error) {
	Db := global.GL_DB

	t := time.Now()
	data.UpdateTime = int(t.Unix())

	//a.GenerateQrcode(a.ID)
	Db = Db.Save(&data)
	return data, Db.Error

}

// Delete 删除
func (a tissue) Delete(ids []int) error {
	Db := global.GL_DB

	return Db.Delete(&a, ids).Error

}

type tissueSelect struct {
	Value    int            `json:"value"`
	Label    string         `json:"label"`
	Children []tissueSelect `json:"children,omitempty"`
}

// Select 远程下拉
func (a tissue) Select(pid int) []tissueSelect {
	Db := global.GL_DB
	var menus []tissue

	Db.Model(&menus).Where("pid=?", pid).Find(&menus)

	var list []tissueSelect

	if len(menus) == 0 {
		return nil
	}
	for _, v := range menus {
		list = append(list, tissueSelect{
			Value:    v.ID,
			Label:    v.Name,
			Children: a.Select(v.ID),
		})
	}

	return list
}

// Selects 远程下拉 pid 不为 0
func (a tissue) Selects(pid int) []tissueSelect {
	var list []tissueSelect
	Db := global.GL_DB
	var menus tissue

	Db.Model(&menus).Where("id=?", pid).First(&menus)
	list = append(list, tissueSelect{
		Value:    menus.ID,
		Label:    menus.Name,
		Children: a.Select(pid),
	})
	return list
}

func (a tissue) GenerateQrcode(id int) {
	Db := global.GL_DB

	Db.Model(&a).Where("id=?", id).Find(&a)

	//生成二维码
	url := "http://bfr.csjwlkj.cn/wap/#/pages/index/index?eid=" + strconv.Itoa(a.ID)
	t := time.Now()
	logoPath := "public/logo.png"
	fileName := a.Name + t.Format("200601021505") + ".png"
	outPath := filepath.Join(tools.Mkdir("public/upload"), "/", fileName)
	err := qrcodes.CreateQrCodeCustomWithLogoPoster(url, logoPath, outPath, qrcode.Highest, 256, a.Name)
	if err != nil {
		log.Println("二维码生成失败：", err)
	}
	a.Qrcode = outPath
	Db.Model(&a).Updates(&a)

}

func (a tissue) GetTissueIds(pid int, Ids []int) []int {
	Db := global.GL_DB
	var menus []tissue

	Db.Model(&menus).Select("id,pid").Where("pid=?", pid).Find(&menus)

	if len(menus) == 0 {
		return Ids
	}
	if len(Ids) == 0 {
		Ids = append(Ids, pid)
	}
	for _, v := range menus {
		Ids = append(Ids, v.ID)
		Ids = a.GetTissueIds(v.ID, Ids)
	}

	return Ids
}

type tissue2 struct {
	model.Tissue
	EnterpriseNum int `json:"enterprise_num"`
	PeopleNum     int `json:"people_num"`
}

// StatList 带统计的列表
func (a tissue) StatList(Db *gorm.DB) (*gorm.DB, []tissue2, int64) {
	var list []tissue2
	var total int64
	Db.Model(&a).
		Select("tissue.*,(select count(id) from enterprise where tissue_id = tissue.id) as enterprise_num," +
			"(select count(id) from user where tissue_id = tissue.id) as people_num").
		Order("id desc").
		Find(&list).Offset(-1).Select("id").Count(&total)
	return Db, list, total
}
